package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by venus on 13/09/14.
 */
public class Library {

    public ArrayList<Book> books = new ArrayList<Book>();
    public ArrayList<Movie> movies = new ArrayList<Movie>();
    public ArrayList<User> users = new ArrayList<User>();
    public static User currentUser = new User("","");
    Menu menu = new Menu();

    public void run() {
        books = SetUpHelper.sampleBooks();
        movies = SetUpHelper.sampleMovies();
        users = SetUpHelper.sampleUsers();

        welcomeMessage();

        //Read Username and Password
        System.out.println("Please login with your credentials:");

        ReaderHelper reader = new ReaderHelper();
        String libraryNumber = reader.getUserInput("Library Number: ");
        String password = reader.getUserInput("Password: ");

        currentUser = login(libraryNumber, password);

        if (currentUser != null)
            menu.displayMenu(this);
        else {
            System.out.println("Invalid LibraryID or Password.");
        }
    }

    public User login(String libraryNumber, String password) {

        for (User user: users){
            if (user.getLibraryNumber().equals(libraryNumber) && user.getPassword().equals(password))
                return user;
        }
        return null;
    }

    public void welcomeMessage() {
        System.out.println("==================================");
        System.out.println(" Welcome to the Bangalore library ");
        System.out.println("==================================");
    }

    public static <T extends ILibraryItem> void displayAvailableItems(ArrayList<T> items) {
        System.out.println("==========================================");
        System.out.println("Here is the list of all available items: ");
        System.out.println("==========================================");

        for(T item: items) {
            if (item.isAvailable()) {
                String movieName = "Name: " + item.getName();
                System.out.println(movieName);
            }
        }
    }

    public static <T extends ILibraryItem> void checkOutItem(T item) {
        System.out.println("Would you like to check out this item? Y/N");

        Scanner input = new Scanner(System.in);
        String answer = input.nextLine();

        if (answer.equals("N")) {
            System.out.println();
        }
        else if (answer.equals("Y")) {
            boolean isAvailable = item.isAvailable();
            if (isAvailable) {
                item.setAvailable(false);
                System.out.println("Thank you. Enjoy the item.");
                currentUser.userBorrowedItems.add(item);
            } else System.out.println("Sorry, item is not available");
        } else {
            System.out.println("Invalid input.");
        }
    }

    public static <T extends ILibraryItem> void searchAndCheckoutItem(ArrayList<T> items) {
        T foundItem = searchItem(items);
        if (foundItem != null) {
            showItemDetails(foundItem);
            checkOutItem(foundItem);
        }
        else
            System.out.println("Sorry, no item found!");
    }

    private static <T extends ILibraryItem> T searchItem(ArrayList<T> items) {
        System.out.println("=====================================");
        System.out.println("Please enter the name of the item: ");
        System.out.println("=====================================");

        Scanner inputBookName = new Scanner(System.in);
        String searchedName = inputBookName.nextLine();

        //Movie found
        for (T item: items) {
            if (item.getName().equals(searchedName)) {
                return item;
            }
        }
        //Movie not found
        return null;
    }

    public static <T extends ILibraryItem> void showItemDetails(T item) {
        System.out.println("===================");
        System.out.println("Here is the Item: ");
        System.out.println("===================");

        String itemDetails = item.provideDetails();
        System.out.println(itemDetails);
    }

    public static <T extends ILibraryItem> void returnItem(ArrayList<T> items) {

        T itemFound = searchItem(items);

        if (itemFound != null) {
            boolean isAvailable = itemFound.isAvailable();
            if (!isAvailable) {
                itemFound.setAvailable(true);
                System.out.println("Thank you for returning this item.");
            } else System.out.println("Item is already here.");
        } else
            System.out.println("Sorry, item not found");
    }

    public static void displayCurrentUserDetails() {
        System.out.println("=======================");
        System.out.println("Here is your details: ");
        System.out.println("=======================");

        String userInfo = currentUser.provideUserDetails();
        System.out.println(userInfo);
    }
}
